﻿using FluentValidation;
using Sgr.UPMS.Application.Commands.Organizations;
using Sgr.UPMS.Application.Commands.UserProfile;
using Sgr.UPMS.Domain.Users;
using Sgr.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sgr.UPMS.Application.Validations.UserProfile
{
    /// <summary>
    /// 忘记密码命令验证器
    /// </summary>
    public class ForgotPasswordCommandValidator : AbstractValidator<ForgotPasswordCommand>
    {
        public ForgotPasswordCommandValidator()
        {
            // 身份验证类型验证
            RuleFor(command => command.AuthenticationType)
                .IsInEnum()
                .WithMessage("无效的身份验证类型")
                .NotEqual(UserAuthenticationType.Password)
                .WithMessage("不支持使用密码方式进行组织机构注册");

            // 验证码验证
            RuleFor(command => command.VerificationCode)
                .NotEmpty()
                .WithMessage("验证码不能为空")
                .Length(6)
                .WithMessage("验证码必须是6位字符");

            // 用途
            RuleFor(command => command.VerificationPurpose)
                .NotEmpty()
                .WithMessage("用途不能为空")
                .Length(4)
                .WithMessage("用途必须是4位字符");

            // 登录密码验证
            RuleFor(command => command.LoginPassword)
                .NotEmpty()
                .WithMessage("登录密码不能为空")
                .Must((pwd) => StringHelper.SgrPasswordComplexityIsCompliant(pwd))
                .WithMessage("密码长度为8-14个字符且数字、字母以及标点符号需至少包含两种，不允许有空格、中文");

            // 验证账号（根据验证类型）
            RuleFor(command => command.Account)
                .NotEmpty()
                .WithMessage("账号不能为空")
                .MaximumLength(50)
                .WithMessage("账号长度不能超过50个字符");

            // 手机号码格式验证
            When(command => command.AuthenticationType == UserAuthenticationType.PhoneNumber, () =>
            {
                RuleFor(command => command.Account)
                    .Must(phone => RegexHelper.IsValidPhoneNumber(phone))
                    .WithMessage("请输入有效的手机号码");
            });

            // 电子邮箱格式验证
            When(command => command.AuthenticationType == UserAuthenticationType.Email, () =>
            {
                RuleFor(command => command.Account)
                    .Must(email => RegexHelper.IsValidEmail(email))
                    .WithMessage("请输入有效的电子邮箱地址");
            });
        }
    }
}